<?php

namespace App\Http\Controllers\Logic;

use App\Http\Controllers\Controller;
use App\Model\OAOrders;
use App\Model\OAProjectLx;
use App\Model\OASupplier;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SupplieLogic extends Controller
{
    public function registerSupplier(&$params)
    {
        if($params['repeatPassword'] != $params['password']) throw new \Exception('两次密码不一致');
        $isset = OASupplier::query()->where('company',$params['company'])->exists();
        if($isset) throw new \Exception('公司'.$params['company'].'已注册');
        $params['created_at'] = date('Y-m-d H:i:s');
        unset($params['repeatPassword']);
        $params['password'] = md5($params['password']);

        return OASupplier::query()->insertGetId($params);
    }

    //获取项目下拉
    public function getProjectList($xmid='')
    {
        $company = (new CommonLogic())->getCommonData(['company']);
        $company = array_column($company['company'],'name','id');
        //dd($company);

        $projectList = OAProjectLx::query()->where('gsid','>',0)->select(['id','name','batch_id','gsid'])->get()->toArray();
        //dd($projectList);
        foreach ($projectList as &$project){
            $project['company'] = $company[$project['gsid']] ?? '';
            unset($project['gsid']);
        }
        //dd($projectList);
        if($xmid){
            $list = array_column($projectList,null,'id');
            return $list[$xmid] ?? [];
        }else{
            return $projectList;
        }
    }

    public function addOrder($input)
    {
        unset($input['xm'],$input['batch_id'],$input['name'],$input['company']);

        $xmid = $input['xmid'];
        $info = OAOrders::query()->where('xmid',$xmid)->first();
        if(is_null($info)){
            $input['created_at'] = date('Y-m-d H:i:s');
            $input['files'] = json_encode($input['files']);
            return ['order_id'=>OAOrders::query()->insertGetId($input),'msg'=>'添加成功'];
        }else{
            $input['updated_at'] = date('Y-m-d H:i:s');
            $input['files'] = json_encode($input['files']);
            unset($input['xmid']);
            OAOrders::query()->where('xmid',$xmid)->update($input);
            return ['order_id'=>$info->id,'msg'=>'修改成功'];
        }
    }


    public function getSupplyList($request)
    {
       return OASupplier::when($request->filled('status') && $request->status,function($q) use ($request){
        return $q->where('status',$request->status);
       })
       ->when($request->filled('account'),function($q) use ($request){
        return $q->where('account','like', "%".$request->account."%");
       })
       ->when($request->filled('company'),function($q) use ($request){
        return $q->where('company','like', "%".$request->company."%");
       })
       ->paginate($request->input('page_size'));
    
    }

    public function editSupply($request)
    {
       $supply = OASupplier::find($request->id);
       if(!$supply->status) throw new \Exception('只能编辑待审核供应商');
       $supply->status = $request->status;
       $supply->reason = $request->reason ?? '';
       $supply->save();
       return $supply->id;

    }
}
